package com.totoro.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * RabbitMQ的帮助类，用于初始化RabbitMQ的环境
 * Created by Niki on 2018/3/28 8:37
 */
public class RabbitMQUtil {
    private RabbitMQUtil() {
    }

    private static String host = "192.168.22.111";
    private static int port = 5672;
    private static String userName = "guest";
    private static String password = "guest";

    private static ConnectionFactory connectionFactory;

    public static ConnectionFactory getConnectionFactory() {
        if (connectionFactory == null) {
            connectionFactory = new ConnectionFactory();
            connectionFactory.setHost(host);
            connectionFactory.setPort(port);
            connectionFactory.setUsername(userName);
            connectionFactory.setPassword(password);
        }
        return connectionFactory;
    }

    public static void closeChannel(Channel channel){
        if(channel != null)
            try {
                channel.close();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (TimeoutException e) {
                e.printStackTrace();
            }
    }

    public static void closeConnection(Connection connection){
        if(connection != null){
            try {
                connection.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
